package com.itqf.utils;

import com.alibaba.fastjson.JSON;
import com.itqf.entity.ScheduleJob;
import com.itqf.exception.MyQuartzException;
import com.itqf.quartz.MyJob;
import org.quartz.*;

import java.util.List;

/**
 * @Description: 1） 创建定时任务  2）修改定时任务   3）删除  4)暂停  5）恢复   6）run
 *
 * @Company: 千锋互联
 * @Author: 李丽婷
 * @Date: 2020/10/21
 * @Time: 上午11:24
 */
public class SchedulerUtils {

    //cronExpression

    /**
     * 创建定时任务
     * @param scheduleJob  代表一个定时任务的对象
     * @param scheduler   调度器
     */
   // public  static  void createTask(String cronExpression, Scheduler scheduler){
    public  static  void createTask(ScheduleJob scheduleJob, Scheduler scheduler){
        //1,创建调度器
        //2,创建触发器
        CronTrigger cronTrigger = TriggerBuilder.newTrigger()
                .withIdentity(SysConstant.TRIGGER_PREFIX+scheduleJob.getJobId()).//2
                 withSchedule(CronScheduleBuilder.
                         cronSchedule(scheduleJob.getCronExpression()))
                .build();

        //3,JobDetail
        JobDetail jobDetail = JobBuilder.newJob(MyJob.class)//
                .withIdentity(SysConstant.JOB_PREFIX+scheduleJob.getJobId()).build();
        //传递schedule_job   beanName  methodName
        String json = JSON.toJSONString(scheduleJob);//quartz
        jobDetail.getJobDataMap().put(SysConstant.DATA_KEY,json);


        //4.注册任务触发器
        try {
            scheduler.scheduleJob(jobDetail,cronTrigger);

            //5.启动
            //scheduler.start();

        } catch (SchedulerException e) {
            e.printStackTrace();
            throw  new MyQuartzException("创建定时任务失败。");
        }


    }

    public  static  void delete(Scheduler scheduler,List<JobKey> jobKeys){

        try {
            scheduler.deleteJobs(jobKeys);
        } catch (SchedulerException e) {
            e.printStackTrace();
            throw  new MyQuartzException("删除定时任务失败。");

        }

    }

    /**
     * 暂停任务
     * @param scheduler
     * @param jobId
     */
    public  static  void pause(Scheduler scheduler,long jobId){

        try {
            JobKey jobKey = JobKey.jobKey(SysConstant.JOB_PREFIX+jobId);
            scheduler.pauseJob(jobKey);
        } catch (SchedulerException e) {
            e.printStackTrace();
            throw  new MyQuartzException("暂停定时任务失败。");

        }

    }
    /**
     * 恢复任务
     * @param scheduler
     * @param jobId
     */
    public  static  void resume(Scheduler scheduler,long jobId){

        try {
            JobKey jobKey = JobKey.jobKey(SysConstant.JOB_PREFIX+jobId);
            scheduler.resumeJob(jobKey);
        } catch (SchedulerException e) {
            e.printStackTrace();
            throw  new MyQuartzException("恢复定时任务失败。");

        }

    }

    /**
     * 运行一次任务
     * @param scheduler
     * @param jobId
     */
    public  static  void runOnce(Scheduler scheduler,long jobId){

        try {
            JobKey jobKey = JobKey.jobKey(SysConstant.JOB_PREFIX+jobId);
            scheduler.triggerJob(jobKey);
        } catch (SchedulerException e) {
            e.printStackTrace();
            throw  new MyQuartzException("运行定时任务失败。");
        }

    }

    /**
     * 修改定时任务  修改cron表达式
     * @param scheduler
     * @param scheduleJob
     */
    public  static  void  update(Scheduler scheduler,ScheduleJob scheduleJob){
        try {
            TriggerKey triggerKey  = TriggerKey.triggerKey(SysConstant.TRIGGER_PREFIX+scheduleJob.getJobId());
            //原来的触发器
            CronTrigger cronTrigger = (CronTrigger) scheduler.getTrigger(triggerKey);
            //构建新的触发器
            CronTrigger newTrigger = cronTrigger.getTriggerBuilder()
                    .withIdentity(triggerKey)
                    .withSchedule
                            (CronScheduleBuilder
                          .cronSchedule(scheduleJob.getCronExpression())).build();
            //注册新的触发器
            scheduler.rescheduleJob(triggerKey,newTrigger);

        } catch (SchedulerException e) {
            e.printStackTrace();
            throw  new MyQuartzException("修改定时任务失败。");

        }


    }




}
